<template>
  <div class="mod-config">
    <center>
      <el-form :inline="true" :model="dataForm">
        <el-form-item>
          <el-input
            v-model="dataForm.key"
            placeholder="输入用户名"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button
            type="primary"
            v-if="purview.add == role.add"
            @click="addOrUpdateHandle()"
            >新增</el-button
          >
          <el-button
            type="danger"
            @click="deleteHandle()"
            v-if="purview.delete == role.delete"
            :disabled="dataListSelections.length <= 0"
            >批量删除</el-button
          >
          <el-button
            type="success"
            @click="excleExport()"
            :disabled="dataListSelections.length <= 0"
            >导出</el-button
          >
          <el-button size="mini" type="primary">
            <el-upload
              class="upload-demo"
              action="/user/import"
              :on-success="uplocadSucess"
              :on-error="uploaderror"
            >
              <el-button size="mini" type="primary">导入</el-button>
            </el-upload>
          </el-button>
        </el-form-item>
      </el-form>
    </center>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="urId"
        header-align="center"
        align="center"
        label="id"
      >
      </el-table-column>
      <el-table-column
        prop="urName"
        header-align="center"
        align="center"
        label="昵称"
      >
      </el-table-column>
      <el-table-column
        prop="urUsername"
        header-align="center"
        align="center"
        label="用户名"
      >
      </el-table-column>

      <el-table-column
        prop="urEmail"
        header-align="center"
        align="center"
        label="邮箱"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="账号的注册时间"
      >
      </el-table-column>
      <el-table-column
        prop="urAvatar"
        header-align="center"
        align="center"
        label="头像"
      >
        <template scope="data">
          <img class="avatar" :src="data.row.urAvatar" />
        </template>
      </el-table-column>
      <el-table-column
        prop="urStuats"
        header-align="center"
        align="center"
        label="状态"
      >
        <template scope="data">
          <el-tag size="small">{{
            data.row.urStuats == 0 ? "冻结" : "正常"
          }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="role.reName"
        header-align="center"
        align="center"
        label="角色"
      >
      </el-table-column>
      <el-table-column
        prop="urSex"
        header-align="center"
        align="center"
        label="性别"
      >
        <template scope="data">
          {{ data.row.urSex == 0 ? "男" : "女" }}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            v-if="purview.update == role.update"
            type="text"
            size="small"
            @click="addOrUpdateHandle(scope.row.urId)"
            >修改</el-button
          >
          <el-button
            v-if="purview.delete == role.delete"
            type="text"
            size="small"
            @click="deleteHandle(scope.row.urId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <center>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        background
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      >
      </el-pagination>
    </center>

    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./user-add-or-update";
import user from "@/api/admin";
import fileDownload from "js-file-download";
import rout from "@/api/routing";
export default {
  data() {
    return {
      role: {
        update: this.$role.role_update,
        delete: this.$role.role_delete,
        add: this.$role.role_add,
      },
      purview: {},
      dataForm: {
        key: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
    };
  },
  components: {
    AddOrUpdate,
  },
  created() {
    this.getDataList();
    this.getPurview();
    //console.log(this.$route.meta.id)     //获取当前页面动态路由的id  明天做权限
  },
  methods: {
    /**
     * 数据导入失败过后的方法
     */
    uploaderror(err, file, fileList) {
      //console.log(err,file,fileList);
      this.getDataList();
    },
    /**
     * 数据导入成功过后的方法
     */
    uplocadSucess(rep, file, fileList) {
      console.log(file, fileList);
      fileList[0] = "";
      this.$message({
        message: rep.message,
        type: "success",
      });
      this.getDataList();
    },
    /**
     * 数据导出
     */
    excleExport(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.urId;
          });
      user.excelport(ids).then((res) => {
        //这里我们已经封装好的方法传入文件的二进制进去就行了和下载过后的文件名
        fileDownload( res,"用户消息.xlsx");
      });
    },

    /**
     * 获取当前页面的权限
     */
    getPurview() {
      rout.purview(this.$route.meta.id).then((res) => {
        this.purview = res.data.data;
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      var parems = {
        page: this.pageIndex,
        limit: this.pageSize,
        key: this.dataForm.key,
      };
      console.log(parems);
      user.list(parems).then(({ data }) => {
        if (data) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.urId;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        user.delte(ids).then(({ data }) => {
          this.getDataList();
        });
      });
    },
  },
};
</script>
<style>
.avatar {
  width: 60%;
  border-radius: 50%;
}
</style>
